var OpenAjax = OpenAjax || {};
OpenAjax.a11y = OpenAjax.a11y || {};
OpenAjax.a11y.cache = OpenAjax.a11y.cache || {};

// Link Object contains information about link on a web page
// OAA rules use this information to analyze
OpenAjax.a11y.cache.link_object = function () {
     var string           = "";
     var text             = "";
     var alt              = "";
     var href             = "";
     var order            = 0;
     var height           = 0;
     var width            = 0;
     var image_only       = false;
     var style_display    = "";
     var style_hidden     = "";
     var style_color      = "";
     var style_background_color = "";
     var node             = null;
} // end OpenAjax.a11y.cache.link_object

//Image Object contains information about an image on a web page
//more fields can be added as necessary
OpenAjax.ally.cache.image_object = function() {
	var src			="";
	var string		="";
	var text		="";
	var alt			="";
	var link		="";
	var height		=0;
	var width		=0;
	var link_only   =false;
	var node		=null;
} //end OpenAjax.a11y.cache.image_object



// ============================
// getAccessibleText
// 
// @node = Object  
// ============================
OpenAjax.a11y.cache.getAccessibleText = function( node ) {
  
  // Create return object
  var o = {};
  var strings = [];
  var alts = [];
  var texts = [];
  o.height = 0;
  o.width = 0;
  o.images = 0;
  getStrings( node, strings, texts, alts);  
  o.string = strings.join("");
  o.text   = texts.join("");
  o.alt    = alts.join("");
  return o;
  
    
  function getStrings( node, strings, tests, images) {
    // If text node get the text and return
    if( node.nodeType == 3 ) {
      var text = node.data;
      strings.push( text );
      tests.push( text );
    } else {
      // if an element for through all the children elements looking for text
      if( node.nodeType == 1 ) {
        // check to see if IMG or AREA element and to use ALT content if defined
        if((node.tagName == 'IMG') || (node.tagName == 'AREA')) {
          var alt = node.getAttribute('ALT');
          // check to make sure alt is defined
          if( alt ) {
            strings.push( alt );
            images.push( alt );
          } // endif
          
          if( node.offsetHeight > o.height ) {
             o.height = node.offsetHeight;
          } //endif
          
          if( node.offsetWidth > o.width ) {
             o.width = node.offsetWidth;
          } //endif
          
          o.images = o.images + 1;
          
        } else {
          for(var n = node.firstChild; n != null; n = n.nextSibling ) {
            getStrings( n, strings, tests, images);
          } // endfor
        } // endif  
      } // endif   
    } // endif
  } // end function getStrings
} // end function OpenAjax.cache.getAccessibleText

// ===================================
// 
//
// ===================================
 
OpenAjax.a11y.cache.updateLinkCache = function( doc ) {

  // Array of link objects
  OpenAjax.a11y.cache.links = [];

  for(var i=0; i < OpenAjax.a11y.cache.links.length; i++ ) {
    OpenAjax.a11y.cache.links.pop()
  }  // endfor
  
  var i=0;
  
  // get a array of A and AREA elements on the page
  getLinks( doc );
  
  return OpenAjax.a11y.cache.links;
  
  function getLinks( node ) {
    // if an element for through all the children elements looking for text
    if( node.nodeType == 1 ) {
      // check to see if A or AREA element and to use ALT content if defined
      if((node.tagName == 'A') || (node.tagName == 'AREA')) {
      
        if( node.href ) {
          
          var o = new OpenAjax.a11y.cache.link_object();
      
          var text_object = OpenAjax.a11y.cache.getAccessibleText(node);
    
          o.order   = i;
          o.string  = text_object.string;
          o.text    = text_object.text;
          o.alt     = text_object.alt;
          o.href    = node.href
          o.node    = node;

          o.height  = node.offsetHeight;
          o.width   = node.offsetWidth;
          o.image_only = false;

          // If the link is an image, use the image height and width
          if( o.height === 0 && o.width === 0 && text_object.height > 0 && text_object.width > 0 ) {
            o.height = text_object.height;
            o.width  = text_object.width;
            o.image_only = true;
          } // endif

          var style = getComputedStyle(node, "");
          
          o.style_display          = style.getPropertyValue("display");
          o.style_hidden           = style.getPropertyValue("hidden");
          o.style_color            = style.getPropertyValue("color");
          o.style_background_color = style.getPropertyValue("background-color");

          if( o.href ) {
            OpenAjax.a11y.cache.links[i] = o;
            i = i + 1;            
          } // endif
          
        }  // endif 
      } // endif        
    } // endif
    
    for(var n = node.firstChild; n != null; n = n.nextSibling ) {
      getLinks( n );
    } // endfor
    
  } // end function getLinks
   
}  // end linkListCache Function



OpenAjax.a11y.cache.updateImagesCache = function( doc ) {

  // Array of image objects
  OpenAjax.a11y.cache.images = [];

  for(var i=0; i < OpenAjax.a11y.cache.images.length; i++ ) {
    OpenAjax.a11y.cache.images.pop()
  }  // endfor
  
  var j=0;
  
  // gets an array of IMG objects from the page
  getImages( doc );
  
  return OpenAjax.a11y.cache.images;
  
  function getImages( node ) {
    // if an element for through all the children elements looking for text
    if( node.nodeType == 1 ) {
      // check to see if Node is IMG element
      if((node.tagName == 'IMG')) {
      
        if( node.src ) {
          
          var i = new OpenAjax.a11y.cache.image_object();
      
          var text_object = OpenAjax.a11y.cache.getAccessibleText(node);
    
		  
          i.src = node.src;
		  i.string = text_object.string;
		  i.text = text_object.text;
		  i.alt = text_object.alr;
		  i.link = node.href;
		  i.height = text_object.height;
		  i.width = text_object.width;
          

          if( i.src ) { // some kind of specification for an image according to requirements
            OpenAjax.a11y.cache.images[j] = i;
            j = j + 1;            
          } // endif
          
        }  // endif 
      } // endif        
    } // endif
    
    for(var n = node.firstChild; n != null; n = n.nextSibling ) {
      getImages( n );
    } // endfor
    
  } // end function getImages
   
}  // end updateImagesCache Function
